home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_07 / 1n07026a < prev    next >
Text File  |  1990-11-03  |  5KB  |  164 lines

  1.  
  2. /*
  3.  *  To link the VESADEMO program, issue the following command:
  4.  *  LINK VESADEMO VESA VESACALL;
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <dos.h>
  11. #include <conio.h>
  12.  
  13. #include "vesa.h"
  14. #include "vesa.fd"
  15.  
  16. extern  int main(int argc,char * *argv);
  17. extern  short SVGAInitializeVideo(short svga_mode,short *old_mode);
  18. extern  void DrawGraphics(void );
  19. extern  void PlotPointColor256(short column,short row,short color);
  20.  
  21. #define USE_MODE 0x100     /* 640x400x256 mode */
  22.  
  23. /* Application-specific error codes */
  24. #define ERROR_VESA_NO_HARDWARE (-3)
  25. #define ERROR_INSUFF_MEMORY    (-4)
  26. #define ERROR_VESA_NO_BIOS     (-5)
  27. #define ERROR_VESA_BAD_MODE    (-6)
  28.  
  29. static VgaInfoBlock demo_vesa_info;
  30. static ModeInfoBlock demo_mode_info;
  31. static char huge *vga_ram;
  32. static long window_granularity;
  33.  
  34. extern  void (far *windowfunc) ();    /* shortcut call to Subfunction 05h*/
  35.  
  36. main(argc, argv)
  37. int argc;
  38. char **argv;
  39. {
  40.    short err;
  41.    short svga_mode;
  42.    short old_mode;
  43.  
  44.    if (argc != 2)
  45.       {
  46.       printf("Usage:  VESADEMO [vesamode]\n\n");
  47.       printf("    where vesamode is 256-color VESA mode below:\n");
  48.       printf("    legal values are 100, 101, 103, 105, and 107\n");
  49.       printf("    For  640 x 400 graphics, use 100\n");
  50.       printf("    For  640 x 480 graphics, use 101\n");
  51.       printf("    For  800 x 600 graphics, use 103\n");
  52.       printf("    For 1024 x 768 graphics, use 105\n");
  53.       printf("    For 1280 x 1024 graphics, use 107\n\n");
  54.       printf("    During the demo, you may press any key to exit\n");
  55.       exit(0);
  56.       }
  57.  
  58.    sscanf(argv[1],"%x",&svga_mode);
  59.    if ((err=SVGAInitializeVideo(svga_mode,&old_mode)) != VESA_OK)
  60.       exit(err);
  61.  
  62.    DrawGraphics();
  63.    getch();
  64.    SetSVGAMode(old_mode, CLEAR_MEMORY_FLAG);
  65.    return 0;
  66. }
  67.  
  68.  
  69. short SVGAInitializeVideo(svga_mode,old_mode)
  70. short svga_mode;
  71. short *old_mode;
  72. {
  73.    short VgaStat;
  74.  
  75.    if ((VgaStat=GetSVGAInfo(&demo_vesa_info)) != VESA_OK)
  76.       {
  77.       printf("Error: VESA support has not been installed!\n");
  78.       return VgaStat;
  79.       }
  80.  
  81.    if ((VgaStat=GetSVGAModeInfo(svga_mode, &demo_mode_info)) != VESA_OK)
  82.       {
  83.       printf("Error: mode %Xh not supported by this VESA driver\n",
  84.              svga_mode);
  85.       return VgaStat;
  86.       }
  87.  
  88.    if ((demo_mode_info.ModeAttributes & SVGA_MODE_HARDWARE) == 0)
  89.       {
  90.       printf("Error: mode %Xh is not supported by your hardware\n",
  91.              svga_mode);
  92.       printf("Please check to make sure you are using the correct monitor.\n");
  93.       return ERROR_VESA_NO_HARDWARE;
  94.       }
  95.  
  96.    if ((demo_mode_info.NumberOfPlanes != 1) ||
  97.         (demo_mode_info.BitsPerPixel != 8) )
  98.       {
  99.       printf("Error: this demonstration program is only designed to\n");
  100.       printf("operate in the 256-color modes provided by your VESA\n");
  101.       printf("driver.\n");
  102.       return ERROR_VESA_BAD_MODE;
  103.       }
  104.  
  105.    if ((demo_mode_info.ModeAttributes & SVGA_MODE_EXTENDED) == 0)
  106.       {
  107.       printf("Error: this program requires extended mode information\n");
  108.       printf("not supplied in your VESA driver.  Contact vendor for\n");
  109.       printf("further assistance.\n");
  110.       return ERROR_VESA_NO_HARDWARE;
  111.       }
  112.  
  113.    GetSVGAModeNo(old_mode);
  114.    SetSVGAMode(svga_mode, CLEAR_MEMORY_FLAG);
  115.    return VESA_OK;
  116. }
  117.  
  118. void DrawGraphics()
  119. {
  120.    short x, y, color, width, height;
  121.  
  122.    vga_ram = (char huge *) ((long)demo_mode_info.WinASegment << 16);
  123.    window_granularity = (long)demo_mode_info.WinGranularity * 1024L;
  124.    windowfunc = demo_mode_info.WinFuncPtr;
  125.  
  126.    width = demo_mode_info.XResolution;
  127.    height = demo_mode_info.YResolution;
  128.  
  129.    for (y=0; y<height; y++)
  130.       for (x=0; x<width; x++)
  131.          {
  132.          color = (short) ((long)x * (long)y);
  133.          PlotPointColor256(x,y,color);
  134.          if (kbhit())
  135.             return;
  136.          }
  137. }
  138.  
  139.  
  140. void PlotPointColor256(column, row, color)
  141. short column;
  142. short row;
  143. short color;
  144. {
  145.    long byte_offset;
  146.    long window_offset;
  147.    long window_pos;
  148.    static long last_window = -1;
  149.  
  150.    byte_offset = (long)row * demo_mode_info.BytesPerScanLine + column;
  151.    window_pos = byte_offset / window_granularity;
  152.    if (last_window != window_pos)
  153.       {
  154. #ifdef DIRECT_WINDOW_CALL
  155.       fastvesa(0, (short)window_pos);
  156. #else
  157.       SelectSVGAMemoryWindow(0, (short)window_pos);
  158. #endif
  159.       last_window = window_pos;
  160.       }
  161.    window_offset = byte_offset % window_granularity;
  162.    vga_ram[window_offset] = (char)color;
  163. }
  164.